//
//  PYStructure.m
//  pyfmdbdemo
//
//  Created by terry peng on 15/12/2.
//  Copyright © 2015年 pengyong. All rights reserved.
//

#import "MHStructure.h"

@implementation MHStructure
- (void)addWithField:(NSString *)field andType:(MHStructureType)type
{
    NSDictionary *structure = [NSDictionary dictionary];
    if (type==MHStructureTypeAutoInc) {
        structure = @{field:@"integer primary key autoincrement"};
    }
    else if (type==MHStructureTypeNormalInt){
        structure = @{field:@"integer not null"};
    }
    else if (type ==MHStructureTypeNormalText){
        structure = @{field:@"text not null"};
    }
    else if(type==MHStructureTypePrimaryInt){
        structure = @{field:@"integer primary key"};
    }
    else if (type==MHStructureTypePrimaryText){
        structure = @{field:@"text primary key"};
    }
    else if (type==MHStructureTypeNormaAllowNullInt){
        structure = @{field:@"interger"};
    }
    else if (type==MHStructureTypeNormalText){
        structure = @{field:@"text"};
    }
    else if (type==MHStructureTypeNormalBolb){
        structure = @{field:@"bolb not null"};
    }
    else if (type==MHStructureTypeNormaAllowNullBolb){
        structure = @{field:@"bolb"};
    }
    [self.structureArray addObject:structure];
}

- (NSDictionary *)structureDictory
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (NSDictionary *dic in self.structureArray) {
        [dict addEntriesFromDictionary:dic];
    }
    return [NSDictionary dictionaryWithDictionary:dict];
}

- (NSMutableArray *)structureArray
{
    if (_structureArray) {
        return _structureArray;
    }
    _structureArray = [NSMutableArray array];
    return _structureArray;
}

- (NSArray *)fieldsArray
{
    return [[self structureDictory] allKeys];
}

- (NSString *)fieldsString
{
    NSString *fields = nil;
    for (NSString *obj in self.fieldsArray) {
        if (fields==nil) {
            fields = obj;
        }
        else{
            fields = [NSString stringWithFormat:@"%@,%@",fields,obj];
        }
    }
    return fields;
}

- (NSDictionary *)dictionaryRepresentationWithTable:(id)table
{
    unsigned int count = 0;
//    objc_property_t *properties = class_copyPropertyList([self class], &count);
    
    NSMutableDictionary *propertyList = [[NSMutableDictionary alloc] init];
//    while (count --> 0) {
//        NSString *key = [NSString stringWithUTF8String:property_getName(properties[count])];
//        id value = [self valueForKey:key];
//        if (value == nil) {
//            propertyList[key] = [NSNull null];
//        } else {
//            propertyList[key] = value;
//        }
//    }
//    free(properties);
//    
    NSMutableDictionary *dictionaryRepresentation = [[NSMutableDictionary alloc] init];
//    [table.columnInfo enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull columnName, NSString * _Nonnull columnDescription, BOOL * _Nonnull stop) {
//        if (propertyList[columnName]) {
//            dictionaryRepresentation[columnName] = propertyList[columnName];
//        }
//    }];
    
    return dictionaryRepresentation;
}

@end
